<template>
  <div class="container">
    <el-dialog
      :title="subId ? '修改学科' : '新增学科'"
      width="30%"
      :visible="showAdd"
      :rules="rules"
      :model='form'
      :before-close="close"
    >
      <el-form ref="fm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="form.subjectName" autofocus></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="form.isFrontDisplay"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="addSub">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// 添加和修改
import { add, update } from '@/api/hmmm/subjects.js'
export default {
  props: {
    showAdd: {
      type: Boolean,
      default: false
    },
    subId: {
      default: () => ({})
    }
  },
  data () {
    return {
      form: {
        addDate: '',
        creatorID: 0,
        id: 1,
        isFrontDisplay: 1,
        subjectName: '',
        tags: 0,
        totals: 0,
        twoLevelDirectory: 0,
        username: ''
      },
      // 校验规则
      rules: {
        subjectName: [{
          required: true,
          message: '学科名称不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    close () {
      this.$emit('closeSub')
      this.form = {
        addDate: '',
        creatorID: 0,
        id: 1,
        isFrontDisplay: 1,
        subjectName: '',
        tags: 0,
        totals: 0,
        twoLevelDirectory: 0,
        username: ''
      }
    },
    // 确认的执行操作
    async addSub () {
      if (this.subId) {
        await update(this.form)
        this.$message.success('修改成功')
        this.$parent.getList()
        this.close()
      } else {
        await add(this.form)
        this.$message.success('新增成功')
        this.$parent.getList()
        this.close()
      }
    },
    getSub (row) {
      this.form = row
    }
  }
}
</script>

<style scoped lang='less'></style>
